<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Orders extends Controller
{
    // 所有订单
    public function index()
    {
        $keyword = input('keyword', ''); // 添加默认值空字符串
        $search = ['query'=>[]];
        $search['query']['keyword'] = $keyword;
        
        $orders = Db::name('orders')
            ->where('orderId|uName', 'like', "%{$keyword}%")
            ->order('orderDate', 'desc')
            ->paginate(10, false, $search);
            
        $this->assign([
            'orders' => $orders,
            'keyword' => $keyword // 确保传递keyword到视图
        ]);
        return view();
    }
    
    // 待付款订单
    public function orders0()
    {
        $orders = Db::name('orders')
            ->where('status', '未付款')
            ->order('orderDate', 'desc')
            ->paginate(10);
            
        $this->assign('orders', $orders);
        return view('index');
    }
    
    // 待发货订单
    public function orders1()
    {
        $orders = Db::name('orders')
            ->where('status', '已付款')
            ->order('orderDate', 'desc')
            ->paginate(10);
            
        $this->assign('orders', $orders);
        return view('index');
    }
    
    // 待收货订单
    public function orders2()
    {
        $orders = Db::name('orders')
            ->where('status', '已发货')
            ->order('orderDate', 'desc')
            ->paginate(10);
            
        $this->assign('orders', $orders);
        return view('index');
    }
    
    // 订单详情
    public function detail($id)
    {
        $order = Db::name('orders')->where('orderId', $id)->find();
        $items = Db::table('orderdetails')
            ->alias('o')
            ->join('products p', 'o.pId = p.pId')
            ->where('o.orderId', $id)
            ->field('p.pId, p.pName, o.number, p.pImg, p.pPrice, o.totalPrice')
            ->select();
            
        $sumPrice = 0;
        foreach($items as $item) {
            $sumPrice += $item['totalPrice'];
        }
        
        $this->assign('order', $order);
        $this->assign('items', $items);
        $this->assign('sumPrice', $sumPrice);
        return view();
    }
    
    // 修改订单状态
    public function updateStatus($id)
    {
        $status = input('status');
        if (!in_array($status, ['已付款', '已发货', '已完成'])) {
            $this->error('无效的状态');
        }
        
        Db::name('orders')->where('orderId', $id)->update(['status' => $status]);
        $this->success('状态更新成功');
    }
    
    // 删除订单
    public function delete($id)
    {
        Db::startTrans();
        try {
            // 先检查订单是否存在
            $orderExists = Db::name('orders')->where('orderId', $id)->find();
            if(!$orderExists) {
                $this->success('订单不存在或已被删除');
            }
            
            // 删除订单详情
            $detailResult = Db::name('orderdetails')
                ->where('orderId', $id)
                ->delete();
                
            // 删除订单
            $orderResult = Db::name('orders')
                ->where('orderId', $id)
                ->delete();
                
            // 检查删除是否成功
            if($detailResult === false || $orderResult === false) {
                Db::rollback();
                $this->error("订单删除失败");
            }
            
            Db::commit();
            $this->success('订单删除成功');
        } catch (\Exception $e) {
            Db::rollback();
            
            // 再次检查订单是否真的不存在了
            $checkOrder = Db::name('orders')->where('orderId', $id)->find();
            if(!$checkOrder) {
                $this->success('订单已删除');
            } else {
                $this->error('订单删除失败: ' . $e->getMessage());
            }
        }
    }
}